package cn.sfinger.huifu.p2p.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.sfinger.huifu.p2p.util.security.KeyTool;
import cn.sfinger.huifu.p2p.util.security.PrivateKey;
import cn.sfinger.huifu.p2p.util.security.RSAEncrypt;
import cn.sfinger.huifu.p2p.util.security.ShaHash;

/**
 * 
 * 汇付天下加签、验签工具类
 * 
 * @author michael.wang.54@gmail.com
 * @version 1.0.0
 * @date 2015年12月9日
 */
public final class HuifuUtil {
	private static Logger logger= LoggerFactory.getLogger(HuifuUtil.class);
	
	/**
	 * 
	 * 统一的签名方法
	 * 
	 * @param params
	 * @param keyList
	 * @param privateKey
	 * @return
	 */
	public static Map<String, Object> sign(Map<String,Object> params, String[] keyList, String privateKey){
		String content="";
		for(String key: keyList){
			content+= (params.get(key)==null?"":(String)params.get(key));
		}
		
		params.put("ChkValue", sign(content, privateKey));
		return params;
	}
	
	/**
	 * 
	 * 验证请求是否合法
	 * 针对汇付回调自己的接口和发送通知
	 * 
	 * @param request
	 * @param keyList
	 * @param publicKey
	 * @return
	 */
	public static boolean verify(HttpServletRequest request, String[] keyList, String publicKey){
		if(StringUtil.isBlank(request.getParameter("ChkValue"))) return false;
		@SuppressWarnings("unchecked")
		Map<String, String[]> params= request.getParameterMap();
		String content="";
		for(String key: keyList){
			content+= params.get(key)[0];
		}
		
		return verify(content,request.getParameter("ChkValue"),publicKey);
	}
	
	/**
	 * 验证请求是否合法
	 * 针对汇付同步应答接口的应答报文
	 * 
	 * @param responseJSON
	 * @param keyList
	 * @param publicKey
	 * @return
	 */
	public static boolean verify(JSONObject responseJSON, String[] keyList, String publicKey){
		String content="";
		for(String key:keyList){
			content+= responseJSON.get(key);
		}
		
		return verify(content,(String)responseJSON.get("ChkValue"),publicKey);		
	}
	
	/**
	 * 
	 * 获取签名
	 * 
	 * @param message
	 * @param privateKey
	 * @return
	 */
	public static String sign(String message, String privateKey){
		logger.debug("befor sign, [message]{} [privateKey]{}", message, privateKey);
		PrivateKey priKey= new PrivateKey(privateKey);
		byte[] paramArrayOfByte= message.getBytes();
	    int i = paramArrayOfByte.length;
	    byte[] arrayOfByte1 = paramArrayOfByte;
	    byte[] arrayOfByte2 = new byte[''];

	    RSAEncrypt localRSAEncrypt = new RSAEncrypt(priKey);

	    ShaHash localShaHash = new ShaHash();
	    localShaHash.add(arrayOfByte1, 0, i);
	    arrayOfByte2 = localShaHash.get();

	    String str = localRSAEncrypt.EncryptMessage(arrayOfByte2);
	    logger.debug("[sign]{}", str.toLowerCase());
	    return str.toUpperCase();
	}
	
	/**
	 * 
	 * 验证签名
	 * 
	 * @param message
	 * @param signature
	 * @param publicKey
	 * @return
	 */
	private static boolean verify(String message, String signature, String publicKey){
		PrivateKey priKey= new PrivateKey(publicKey,false);
		
	    byte[] arrayOfByte3 = message.getBytes();
	    int i = arrayOfByte3.length;
	    
	    ShaHash localShaHash = new ShaHash();
	    localShaHash.add(arrayOfByte3, 0, i);
	    byte[] arrayOfByte2 = localShaHash.get();

	    String str1 = KeyTool.decryptMessage(signature, priKey);
	    String str2 = KeyTool.toStringBlock(arrayOfByte2);
	    return str2.equalsIgnoreCase(str1); 
	}	
}
